package com.leyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.BrandDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.entity.Brand;
import com.leyou.mapper.BrandMapper;
import com.leyou.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    //    分页
    @Override
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
//        分页
        PageHelper.startPage(page, rows);
//        key:模糊查询
        //        动态sql
        Example example = new Example(Brand.class);
//        拼接查询条件
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(key)) {
//            不空
            criteria.andLike("name", "%" + key + "%");

        }

//      添加排序的内容
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));
        }
//        null表示全查
        List<Brand> select = brandMapper.selectByExample(example);

//        没有查到数据
        if (CollectionUtils.isEmpty(select)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

//        统计元素的总个数和页数
        PageInfo<Brand> pageInfo = new PageInfo<>(select);

        return new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), BeanHelper.copyWithCollection(select, BrandDTO.class));


    }

//    添加数据
    @Transactional  //修改多个表
    @Override
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {
//        转成与数据库交互的实体类
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
//        保存品牌brand同时主键回写
        int count = brandMapper.insertSelective(brand);
        if(count != 1){
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        // 新增品牌和分类中间表
        count = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if(count != cids.size()){
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }


    }

//    内容的修改
    @Override
    @Transactional //开启事务管理
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
//        新增数据
        int count = brandMapper.updateByPrimaryKeySelective(brand);
        if (count != 1){
            throw new LyException(ExceptionEnum.UPDATE_GENGXIN_FAIL);
        }

//        删除中间表
        brandMapper.deleteCategoryBrand(brand.getId());

//       新增中间表数据
        count = brandMapper.insertCategoryBrand(brand.getId(),cids);
        if (count != cids.size()){
//            新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }

//    根据品牌id查询品牌
    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

//    根据分类查询品牌
    public List<BrandDTO> queryByCategoryId(Long categoryId) {
        List<Brand> list = brandMapper.queryByCategoryId(categoryId);
        // 判断是否为空
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

//    根据查询品牌对象
    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> cids) {
//        查询品牌
        List<Brand> brands = this.brandMapper.selectByIdList(cids);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }


        return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }
}
